﻿mainModule.controller("simpleEditCtrl", ["$scope", "$rootScope", "itemSvc", "notification", "pageState", "columnSvc", "$routeParams", "cache", "articleSvc", function ($scope, $rootScope, itemSvc, notification, pageState, columnSvc, $routeParams, cache, articleSvc) {
    var mode;
    var articleID = $routeParams.articleID;
    var article = {};    

    setAddMode();    

    function setAddMode() {        
        mode = "add";
        $scope.item = { ArticleID: articleID };
    }

    $scope.$on('itemAdding', function () {
        setAddMode();
    });

    $scope.invalidSave = false;
    $scope.save = function () {       
        $scope.savedPressed = true;
       
        if ($scope.addForm.$valid) {
            itemSvc.add($scope.item).then(function (response) {
                if (response.data.Success) {
                    $scope.item.ID = response.data.Value;
                    $scope.$emit('itemAdded', $scope.item);
                    $scope.savedPressed = false;
                }

                notification.notify(response.data);
                setAddMode();
            });
        }
    }
}])